<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="7">
        <el-card>
          <el-form
            :rules="rules"
            :inline="true"
            ref="conncectFormRef"
            :model="connectForm"
            class="form-contain"
            size="small"
          >
            <el-form-item
              :label="t('plc.ip')"
              prop="ip"
            >
              <el-input
                v-model="connectForm.ip"
                :disabled="plcClientStatus"
                :placeholder="t('plc.serverIp')"
                @keyup.enter="connect(conncectFormRef)"
              />
            </el-form-item>
            <el-form-item
              :label="t('plc.port')"
              prop="port"
            >
              <el-input
                type="number"
                v-model.number="connectForm.port"
                :disabled="plcClientStatus"
                :placeholder="t('plc.serverPort')"
                @keyup.enter="connect(conncectFormRef)"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                :type="plcClientStatus ? 'danger' : 'primary'"
                @click="connect(conncectFormRef)"
                :loading="loading"
              >
                {{ t(plcClientStatus ? 'plc.disconnect' : 'plc.connect') }}
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 根据消息类型展示不同的表单 -->
          <el-tabs
            type="border-card"
            v-model="activeName"
            class="demo-tabs"
            @tab-click="handleClick"
          >
            <el-tab-pane
              label="RC"
              name="RC"
            >
              <RcMessage />
            </el-tab-pane>
            <el-tab-pane
              label="MO"
              name="MO"
            >
              <MoMessage />
            </el-tab-pane>
            <el-tab-pane
              label="SL"
              name="SL"
            >
              <SlMessage />
            </el-tab-pane>
            <el-tab-pane
              label="RP"
              name="RP"
            >
              <RpMessage />
            </el-tab-pane>
            <el-tab-pane
              label="通用"
              name="通用"
            >
              <ComClient />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
      <el-col :span="17">
        <Log />
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import {
  ElMessage,
  FormInstance,
  FormRules,
  TabsPaneContext,
} from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import MoMessage from './components/MoMessage.vue'
import RcMessage from './components/RcMessage.vue'
import SlMessage from './components/SlMessage.vue'
import RpMessage from './components/RpMessage.vue'
import ComClient from './components/ComClient.vue'
import Log from '@/components/Log/plcLog.vue'
import { closeClient, creatClient } from '@/api/plc'
import { useI18n } from 'vue-i18n'
import { plcStore } from '@/store/plcStore'
import { storeToRefs } from 'pinia'

const { t } = useI18n()

const activeName = ref('MO')

const loading = ref(false)

const store = plcStore()

const { plcClientStatus, plcClientIp, plcClientPort } = storeToRefs(store) // 连接状态

// 连接信息表单
const connectForm = reactive({
  ip: plcClientIp.value,
  port: plcClientPort.value,
})

// 表单校验规则
const rules = reactive<FormRules>({
  ip: [{ required: true, message: '请填写连接IP', trigger: 'blur' }],
  port: [
    {
      required: true,
      message: '请填写连接端口',
      trigger: 'change',
    },
    { type: 'number', message: '端口号必须为数字类型' },
  ],
})

// 连接
const conncectFormRef = ref<FormInstance>()
const connect = async (formEl: FormInstance | undefined) => {
  // 储存ip和port，解决刷新丢失
  store.setPlcClientIp(connectForm.ip)
  store.setPlcClientPort(connectForm.port)

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true

      if (plcClientStatus.value) {
        // 断开连接
        closeClient()
          .then((res) => {
            store.setPlcClientStatus(false)
          })
          .finally(() => {
            loading.value = false
          })
      } else {
        // 建立连接
        creatClient(connectForm.ip, connectForm.port)
          .then((res) => {
            store.setPlcClientStatus(true)
          })
          .finally(() => {
            loading.value = false
          })
      }
    }
  })
}

const handleClick = (tab: TabsPaneContext, event: Event) => {
  // console.log(tab, event)
}
</script>
<style scoped lang="scss">
.form-contain {
  margin-bottom: 20px;
}
</style>
